package com.java.module.fire.controller.api;

import com.java.common.lang.annotation.Log;
import com.java.common.lang.domain.AjaxResult;
import com.java.common.lang.domain.page.Page;
import com.java.common.lang.domain.page.TableDataInfo;
import com.java.common.lang.enums.BusinessTypeEnum;
import com.java.common.service.ApiTokenService;
import com.java.common.utils.WebKit;
import com.java.module.fire.domain.Like;
import com.java.module.fire.domain.Post;
import com.java.module.fire.service.IPostService;
import jodd.jtx.meta.Transaction;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.java.module.fire.service.ILikeService;

import java.util.List;

/**
 * 收藏点赞Controller
 * 
 * @author Administrator
 * @date 2025-02-10
 */
@RestController("ApiLikeController")
@RequestMapping("/fire/like")
public class LikeController {

    @Autowired
    ILikeService likeService;

    @Autowired
    ApiTokenService apiTokenService;

    @Autowired
    IPostService postService;



    /**
     * 新增收藏点赞
     */
    @Log(title = "收藏点赞关注", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/addLike")
    @Transaction
    public AjaxResult add(@RequestBody Like like) {

        if(like.getPostId()==apiTokenService.getLoginUserId()){
            return AjaxResult.error("不能关注自己");
        }


        like.setUid(apiTokenService.getLoginUserId());
        List<Like> likeList = likeService.selectLikeList(like);
        if (likeList != null && likeList.size() > 0) {
            likeService.deleteLikeById(likeList.get(0).getLikeId());
            if(like.getType() == 1){
                Post post = new Post();
                post.setPostId(like.getPostId());
                post.setLikeCount(-1L);
                postService.addLike(post);
            }
            if(like.getType() == 2){
                Post post = new Post();
                post.setPostId(like.getPostId());
                post.setCommentCount(-1L);
                postService.addComment(post);
            }
        }else{
            likeService.insertLike(like);
            if(like.getType() == 1){
                Post post = new Post();
                post.setPostId(like.getPostId());
                post.setLikeCount(1L);
                postService.addLike(post);
            }
            if(like.getType() == 2){
                Post post = new Post();
                post.setPostId(like.getPostId());
                post.setCommentCount(1L);
                postService.addComment(post);
            }
        }


        return AjaxResult.success("操作成功");
    }




    @GetMapping("/list")
    public Page<Like> list(Like like) {
        WebKit.startPage();
        like.setUid(apiTokenService.getLoginUserId());
        List<Like> list = likeService.selectLikeListPost(like);
        return WebKit.getPageData(list);
    }


    @GetMapping("/userlist")
    public Page<Like> userlist(Like like) {
        WebKit.startPage();
        like.setPostId(apiTokenService.getLoginUserId());
        List<Like> list = likeService.selectLikeListUser(like);
        return WebKit.getPageData(list);
    }

}